package com.flavors.wan.main.ui.Activity;

import android.view.View;

import androidx.viewpager.widget.ViewPager;

import com.flavors.wan.main.FixedFragmentPagerAdapter;
import com.flavors.wan.main.ui.BaseActivity;
import com.flavors.wan.main.ui.Fragment.FourFragment;
import com.flavors.wan.main.ui.Fragment.OneFragment;
import com.flavors.wan.main.ui.Fragment.ThreeFragment;
import com.flavors.wan.main.ui.Fragment.TwoFragment;
import com.flavors.wan.main.view.TabView;
import com.plat.sdk.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ydong
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.wan_vp)
    ViewPager mVp;

    @BindView(R.id.tab_weixin)
    TabView mTabWeixin;

    @BindView(R.id.tab_contact)
    TabView mTabContact;

    @BindView(R.id.tab_find)
    TabView mTabFind;

    @BindView(R.id.tab_profile)
    TabView mTabProfile;

    List<TabView> mTabViews = new ArrayList<>();

    private static final int INDEX_WEIXIN = 0;
    private static final int INDEX_CONTACT = 1;
    private static final int INDEX_FIND = 2;
    private static final int INDEX_PROFILE = 3;
    private FixedFragmentPagerAdapter pagerAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mTabViews.add(mTabWeixin);
        mTabViews.add(mTabContact);
        mTabViews.add(mTabFind);
        mTabViews.add(mTabProfile);

        pagerAdapter = new FixedFragmentPagerAdapter(getSupportFragmentManager());
        pagerAdapter.setFragmentList(
                OneFragment.newInstance(),
                TwoFragment.newInstance(),
                ThreeFragment.newInstance(),
                FourFragment.newInstance()
        );
        mVp.setAdapter(pagerAdapter);
        mVp.setOffscreenPageLimit(3);
        mVp.setCurrentItem(3);
        mVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            /**
             * @param position 滑动的时候，position总是代表左边的View， position+1总是代表右边的View
             * @param positionOffset 左边View位移的比例
             * @param positionOffsetPixels 左边View位移的像素
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // 左边View进行动画
                mTabViews.get(position).setXPercentage(1 - positionOffset);
                // 如果positionOffset非0，那么就代表右边的View可见，也就说明需要对右边的View进行动画
                if (positionOffset > 0) {
                    mTabViews.get(position + 1).setXPercentage(positionOffset);
                }
            }
        });
    }

    @Override
    protected void loadData() {

    }


    @OnClick({R.id.tab_weixin, R.id.tab_contact, R.id.tab_find, R.id.tab_profile})
    public void onClickTab(View v) {
        switch (v.getId()) {
            case R.id.tab_weixin:
                mVp.setCurrentItem(INDEX_WEIXIN, false);
                updateCurrentTab(INDEX_WEIXIN);
                break;
            case R.id.tab_contact:
                mVp.setCurrentItem(INDEX_CONTACT, false);
                updateCurrentTab(INDEX_CONTACT);
                break;

            case R.id.tab_find:
                mVp.setCurrentItem(INDEX_FIND, false);
                updateCurrentTab(INDEX_FIND);
                break;

            case R.id.tab_profile:
                mVp.setCurrentItem(INDEX_PROFILE, false);
                updateCurrentTab(INDEX_PROFILE);
                break;
        }
    }

    private void updateCurrentTab(int index) {
        for (int i = 0; i < mTabViews.size(); i++) {
            if (index == i) {
                mTabViews.get(i).setXPercentage(1);
            } else {
                mTabViews.get(i).setXPercentage(0);
            }
        }
    }

}

